#!/usr/bin/env python
# coding: utf-8

# In[45]:


import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score
from sklearn.linear_model import LogisticRegression as LR
from sklearn.preprocessing import StandardScaler #标准化
from sklearn.model_selection import GridSearchCV #网格搜索
from sklearn.metrics import precision_score, recall_score, f1_score


# # 导入数据

# In[7]:


iris = load_iris()
# print(iris)
X = iris.data
y = iris.target


# # 切分数据集

# In[28]:


Xtrain, Xtest, Ytrain, Ytest = train_test_split(X,y,random_state=420)


# # 使用标准化包，对训练集来学习，从而对训练集和测试集来做标准化

# In[31]:


#对训练集和测试集做标准化---去量纲
std = StandardScaler().fit(Xtrain)
Xtrain_ = std.transform(Xtrain)
Xtest_ = std.transform(Xtest)

clf = LR()    
lr = clf.fit(Xtrain_, Ytrain)


# # 在确定l2范式的情况下，使用网格搜索判断solver，c的最优组合

# In[38]:


#在l2范式下，判断C和solver的最优值
p = {
    'C':list(np.linspace(0.05,1,19)),
    'solver':['liblinear','sag','newton-cg','lbfgs']
}

model = LR(penalty='l2',)

GS = GridSearchCV(model,p,cv=5)
GS.fit(Xtrain_,Ytrain)


# In[33]:


print('最优组合', GS.best_params_)


# # 将最优的结果重新用来实例化模型，查看训练集和测试集下的分数

# In[51]:


model = LR(penalty='l2',
           C=GS.best_params_['C'],
           solver=GS.best_params_['solver'])

model.fit(Xtrain_,Ytrain)
print(model.score(Xtrain_,Ytrain), model.score(Xtest_,Ytest))


# # 计算精准率

# In[53]:


precision_score(y,model.predict(X), average='micro')


# In[ ]:




